var app = getApp()
var verify = require('../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      commodityPriceId: options.commodityPriceId
    })
  },

  getPriceDetail() {
    var commodityPriceId = this.data.commodityPriceId

    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this
    wx.request({
      url: baseUrl + 'vending/commodity_price/applet/' + commodityPriceId,
      header: {
        'AUTHORIZATION': token
      },
      success: function(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var priceDetail = res.data.data
          // console.log("priceDetail = ", priceDetail)

          that.setData({
            priceDetail: priceDetail
          })


        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },
  getCommodityDetail() {
    var commodityPriceId = this.data.commodityPriceId;
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this
    wx.request({
      url: baseUrl + 'vending/commodity_price/' + commodityPriceId + '/commodity_goods_aisle/list',
      header: {
        'AUTHORIZATION': token
      },
      success: function(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          let commodityData = res.data.data;
          for (let i = 0; i < commodityData.length; i++) {
            commodityData[i].commodityPrice = commodityData[i].commodityPrice.toFixed(2);
          }
          that.setData({
            commodityData: commodityData
          })


        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 修改价格
  editPrice(res) {
    var commodityPriceId = this.data.commodityPriceId;
    var commodityId = res.currentTarget.dataset.gsid;
    if(!commodityId){
      wx.showModal({
        title: '提示',
        content: '此货道暂未配置商品，请在平台配置完成后，才可进行修改',
      })
    }else{
      wx.navigateTo({
        url: 'onecmd-detail/onecmd-detail?commodityPriceId=' + commodityPriceId +
          '&commodityId=' + commodityId
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function() {
    this.getPriceDetail();
    this.getCommodityDetail();
  },
})